﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Data;

/// <summary>
/// Summary description for Class1
/// </summary>
public class ConvertHtmlMimeToXml
{
    public ConvertHtmlMimeToXml()
	{
		//
		// TODO: Add constructor logic here
		//
	}



    public static Dictionary<string,string> convert(string htmlFileName, string xmlFileName)
    {
        if (htmlFileName == null)
            return new Dictionary<string, string>();

        xmlFileName = xmlFileName == null ? "" : xmlFileName;

        XmlDocument xDocument = new XmlDocument();
        try
        {
            xDocument.Load(htmlFileName);
        }
        catch
        {
            return new Dictionary<string,string>();
        }

        DataSet dsNew = new DataSet();
        dsNew.Tables.Add(new DataTable("mime"));
        dsNew.Tables[0].Columns.Add(new DataColumn("EXT", typeof(String)));
        dsNew.Tables[0].Columns.Add(new DataColumn("MIME", typeof(String)));

        if (xDocument.ChildNodes.Count < 2)
            return new Dictionary<string, string>();

        XmlNode xnMainNode = xDocument.ChildNodes[1];

        


        foreach (XmlNode xnTr in xnMainNode.ChildNodes)
        {
            String [] arrRowData = getRowData (xnTr);

            if (arrRowData.Length >= 2)
            {
                DataRow dr = dsNew.Tables[0].NewRow();
                dr["EXT"] = arrRowData[0];
                dr["MIME"] = arrRowData[1];

                dsNew.Tables[0].Rows.Add(dr);
            }

            


        }


        Dictionary<string, string> drRet = new Dictionary<string, string>();

        foreach (DataRow dr in dsNew.Tables[0].Rows)
        {
            try
            {
                drRet.Add(dr["EXT"].ToString(), dr["MIME"].ToString());
            }
            catch
            {
            }
            
        }


        try
        {
            if (xmlFileName != "")
            {
                XmlDocument xmlNewDocument = new XmlDocument();
                xmlNewDocument.AppendChild( xmlNewDocument.CreateXmlDeclaration("1.0", "utf-8", "yes"));
                xmlNewDocument.AppendChild(xmlNewDocument.CreateElement("mimetypes"));

                foreach (String key in drRet.Keys)
                {
                    
                    XmlNode xnKey = xmlNewDocument.CreateElement("MIMETYPE");
                    XmlAttribute xa = xmlNewDocument.CreateAttribute("EXT");
                    xa.Value = key;
                    xnKey.Attributes.Append(xa);
                    xa = xmlNewDocument.CreateAttribute("MIME");
                    xa.Value = drRet[key];
                    xnKey.Attributes.Append(xa);
                    
                    xmlNewDocument.ChildNodes[1].AppendChild(xnKey);

                }


                


                xmlNewDocument.Save(xmlFileName);
            }
        }
        catch (Exception ex)

        {
        }



        return drRet;


        


    }

    private static string[] getRowData(XmlNode xnTr)
    {
        List<string> lstRet = new List<string> ();
          foreach (XmlNode xmlTd in xnTr.ChildNodes)
          {
              if (xmlTd.Name.ToLower() == "td")
              {
                  if (xmlTd.ChildNodes.Count > 0)
                  {
                      if (xmlTd.ChildNodes[0].NodeType == XmlNodeType.Text)
                      {
                          String value = xmlTd.ChildNodes[0].Value;
                          
                         
                          lstRet.Add(value);
                      }
                  }
              }
          }





          if (lstRet.Count >= 2)
          {
              if (lstRet[0].Length > 1)
                  if (lstRet[0][0] == '.')
                      lstRet[0] = lstRet[0].Substring(1, lstRet[0].Length - 1);
              lstRet[0] = lstRet[0].ToLower();
              return lstRet.ToArray();
          }
          else
              return new String[0];
    }



    public static Dictionary<string, string> readData(String xmlFileName)
    {
        Dictionary<string, string> drRet = new Dictionary<string, string>();
        XmlDocument xmlNew = new XmlDocument();
        try
        {
            xmlNew.Load(xmlFileName);
        }
        catch
        {
            return new Dictionary<string, string>();
        
        }


        if (xmlNew.ChildNodes.Count < 2)
            return new Dictionary<string, string>();

        foreach (XmlNode xmlIn in xmlNew.ChildNodes[1].ChildNodes)
        {
            if (xmlIn.Attributes["EXT"] != null && xmlIn.Attributes["MIME"] != null)
            {
                drRet.Add(xmlIn.Attributes["EXT"].Value, xmlIn.Attributes["MIME"].Value);
            }
        }
        return drRet;
    }
}